﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.PO;

namespace Com.CMS263.DAO
{
    public class ExtValueDAO:BaseService 
    {
        public void Add(ExtValue ev)
        {
            DBHelper.TransSaveOrUpdate(ev);
        }
        public void Modify(ExtValue ev)
        {
            DBHelper.TransUpdate(ev);
        }
        public void DeleteByBaseArtId(int artId)
        {

        }
        /// <summary>
        /// 根基基本内容编号得到所有扩展属性的值扩展
        /// </summary>
        /// <param name="id">基本属性编号</param>
        /// <returns></returns>
        public IList<ExtValue> GetExtValueByBaseArtId(int id)
        {
            return DBHelper.ExecuteHQL("from ExtValue where baseArtId=" + id).List<ExtValue>();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="artId"></param>
        /// <param name="attrId"></param>
        /// <returns></returns>
        public string GetValueById(int artId, int attrId)
        {
            try
            {
                return DBHelper.ExecuteHQL("select val from  ExtValue where baseArtId=" + artId + " and attrId=" + attrId + "").UniqueResult<string>();
            }
            catch (Exception)
            {
                return "";
            }
            
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="artId"></param>
        /// <param name="attrId"></param>
        /// <returns></returns>
        public ExtValue GetExtValueById(int artId, int attrId)
        {
            try
            {
                return DBHelper.ExecuteHQL("from  ExtValue where baseArtId=" + artId + " and attrId=" + attrId).List<ExtValue>().First();
            }
            catch (Exception)
            {
                return null;
            }
        }

        #region 根据AttrId查询信息条数
        /// <summary>
        /// 根据AttrId查询信息条数
        /// </summary>
        /// <param name="attrid"></param>
        /// <returns></returns>
        public int GetCounByAttrId(int attrid)
        {
            string hql = "select count(*) from ExtValue where AttrId="+attrid;
            return Convert.ToInt32(GetValue(hql));
        }
        #endregion
    }
}
